package com.example.rg.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.rg.generator.commons.CustomException;
import com.example.rg.generator.domain.Category;
import com.example.rg.generator.domain.Dish;
import com.example.rg.generator.domain.Setmeal;
import com.example.rg.generator.service.CategoryService;
import com.example.rg.generator.mapper.CategoryMapper;
import com.example.rg.generator.service.DishService;
import com.example.rg.generator.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author DABB
 * @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
 * @createDate 2023-04-28 09:16:00
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    //删除分类
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> lqwDish = new LambdaQueryWrapper<>();
        //根据id查询菜品数据
        lqwDish.eq(Dish::getCategoryId, id);
        int dishcount = dishService.count(lqwDish);
        //如已有关联弹出错误
        if (dishcount > 0) {
            throw new CustomException("当前分类关联了菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> lqwSetmeal = new LambdaQueryWrapper<>();
        //根据id查询套餐数据
        lqwSetmeal.eq(Setmeal::getCategoryId, id);
        int setmealcount = setmealService.count(lqwSetmeal);
        //如已有关联弹出错误
        if (setmealcount > 0) {
            throw new CustomException("当前分类关联了套餐，不能删除");
        }

        //正常删除分类
        super.removeById(id);
    }
}




